﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ShortcutData.cs" company="TechBits">
//   Copyright (c) TechBits. All rights reserved.
// </copyright>
// <author>
//   Leandro G. Vacirca
// </author>
// <summary>
//   Defines the ShortcutData type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IntelliShortCut.Data
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Xml.Serialization;

    /// <summary>
    /// This class contains the data of a shortcut.
    /// </summary>
    [Serializable]
    public class ShortcutData : ICloneable
    {
        /// <summary>
        /// Gets or sets the Id.
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// Gets or sets MD5Key.
        /// </summary>
        public string MD5Key { get; set; }

        /// <summary>
        /// Gets or sets the main name of the shortcut.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the alias of the shortcut.
        /// </summary>
        public string Alias { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether ByNameOrAlias.
        /// </summary>
        public bool ByNameOrAlias { get; set; }

        /// <summary>
        /// Gets or sets the full path of the shortcut.
        /// </summary>
        public string FullPath { get; set; }

        /// <summary>
        /// Gets or sets the category of the shortcut.
        /// </summary>
        public ShortcutCategoryEnum Category { get; set; }

        /// <summary>
        /// Gets or sets Rank.
        /// </summary>
        public int Rank { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether Active.
        /// </summary>
        public bool Active { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether Valid.
        /// </summary>
        public bool Valid { get; set; }

        /// <summary>
        /// Gets or sets Last date.
        /// </summary>
        public DateTime LastUsed { get; set; }

        /// <summary>
        /// Gets or sets the icon of the shortcut.
        /// </summary>
        [XmlIgnore]
        public Bitmap Icon { get; set; }

        /// <summary>
        /// Gets or sets PictureByteArray.
        /// </summary>
        [XmlElementAttribute("Icon")]
        public byte[] PictureByteArray
        {
            get
            {              
                if (Icon != null)
                {
                    TypeConverter bitmapConverter = TypeDescriptor.GetConverter(Icon.GetType());
                    return (byte[])bitmapConverter.ConvertTo(Icon, typeof(byte[]));
                }

                return null;
            }

            set 
            {
                Icon = value != null ? new Bitmap(new MemoryStream(value)) : null;
            }
        }

        /// <summary>
        /// Updates the shortcut data.
        /// </summary>
        /// <param name="shortcut">The shortcut.</param>
        public void Update(ShortcutData shortcut)
        {
            Id = shortcut.Id;
            MD5Key = shortcut.MD5Key;
            Name = shortcut.Name;
            Alias = shortcut.Alias;
            ByNameOrAlias = shortcut.ByNameOrAlias;
            FullPath = shortcut.FullPath;
            Category = shortcut.Category;
            Active = shortcut.Active;
            Rank = shortcut.Rank;
            Valid = shortcut.Valid;
            LastUsed = shortcut.LastUsed;
        }

        /// <summary>
        /// Gets a string with the shortcuts data.
        /// </summary>
        /// <returns>
        /// String with the shortcuts data.
        /// </returns>
        public override string ToString()
        {
            return string.Format("Name={0} - Alias={1} ByNameOrAlias={2} - FullPath={3} - Category={4} - Active={5} - Rank={6}", Name, Alias, ByNameOrAlias, FullPath, Category, Active, Rank);
        }

        /// <summary>
        /// Clones ShortcutData.
        /// </summary>
        /// <returns>
        /// A cloned ShortcutData object.
        /// </returns>
        public object Clone()
        {
            return MemberwiseClone();
        }
    }
}